<?php
$this->breadcrumbs=array(
	'User Roles'=>array('index'),
	'Manage',
);

$this->menu=array(
	array('label'=>'Manager UserRole', 'url'=>array('admin')),
	array('label'=>'Create UserRole', 'url'=>array('create')),
);

$this->mainTitle = Util::t('User Role Manager');
?>

<h1><?php echo Util::t('User Role Manager'); ?></h1>

<div>
    <?php foreach ($listRoles as $role => $value): ?>
    <input type="radio" name="role-type" value="<?php echo $value->itemname; ?>"/> <span><?php echo $value->itemname; ?></span>
    <?php endforeach;?>
</div>
<br/>
<div id="user-in-role"></div><br/>
<div id="role-area"></div>
<img src="<?php  echo Yii::app()->theme->baseUrl."/images/loading.gif"; ?>" class="hide"/>

<script type="text/javascript">
    $().ready(function(){
        $("input[name=role-type]").click(function(){
           $.ajax({
               url: "<?php echo CController::createUrl('userRole/loadUserInRole'); ?>",
               type: "post",
               data: "role_name="+$(this).val(),
               success: function(data){
                   $("div#user-in-role").html(data);
                   $("div#role-area").html("");
               }
           })
        });
        
        $("select#user-in-role").live('change', function(){
            $.ajax({
               url: "<?php echo CController::createUrl('userRole/roleCheck'); ?>",
               type: "post",
               data: "role="+$("input[name=role-type]:checked").val()+"&user_id="+$("select#user-in-role").val(),
               beforeSend: function(){
                   $("img.hide").show();
               },
               success: function(data){
                   $("div#role-area").html(data);
                   $("img.hide").hide();
               }
           })
        });
        
        $("a.check-all-role").live('click', function(){
            var controller_name_term = ($(this).text());            
            $("input[type=checkbox]").each(function(){
                var key = $(this).attr('id').substr(0, $(this).attr('id').indexOf("_"));    //get a string in id of obj (Brand_create -> Brand)
                if (key == controller_name_term){
                    $(this).attr('checked', true);                    
                }
            }); 
        });
        
        $("input[type='checkbox']").live('click', function(){
            var controllerName = $(this).attr('id').substr(0, ($(this).attr('id').indexOf('_')));
            var controllerAction = $(this).attr('id').substr($(this).attr('id').indexOf('_')+1);
            var status = ($(this).is(":checked"));
            
            $.ajax({
                url: "<?php echo CController::createUrl('userrole/updateUserRole'); ?>",
                type: "post",
                data: "user_id="+$("select#user-in-role").attr('value')+"&controller_name="+controllerName+"&controller_action="+controllerAction+"&status="+status+"&role="+$("input[name='role-type']:checked").val(),
                beforeSend: function(){},
                success: function(){alert('Updated User Role Success')},
                error: function(){alert('error')}
            });
        });
    })
</script>


